<template>
	<view>
		<view>
			<!-- 条件选择器 -->
			<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="text" />
			<uni-search-bar bgColor="white" placeholder="请输入名称或标题" @confirm="search" @cancel="reset"
				@clear="reset" />
			<!-- 新闻 -->
			<view v-if="current==0"
				style="display: flex;justify-content: space-around;border-bottom: 1px solid #C0C0C0;padding-bottom: 12rpx;margin-top: 20rpx;margin-bottom: 20rpx;"
				v-for="(n,index1) in newsInfo" :key="index1" @click="navigateTo(n.newId,0)">
				<view style="width: 200rpx;height: 160rpx;"><img style="width: 200rpx;height: 160rpx;"
						:src="n.newAvatar" alt="">
				</view>
				<view style="width: 65%;">
					<view>{{n.newTitle}}</view>
					<view style="font-size: smaller;color: #808080;margin-top: 10rpx;margin-bottom: 10rpx;">
						{{n.newOrganizationName}}
					</view>
					<view
						style="display: flex;justify-content: space-between;font-size: smaller;color: #808080;margin-top: 40rpx;">
						<view>{{n.newViews}}阅读</view>
						<view>{{n.createAt}}</view>
					</view>
				</view>
			</view>
			<!-- 活动 -->
			<view v-if="current==1"
				style="display: flex;justify-content: space-around;border-bottom: 1px solid #C0C0C0;padding-bottom: 12rpx;margin-top: 20rpx;margin-bottom: 20rpx;"
				v-for="(a,index2) in activityInfo" :key="index2" @click="navigateTo(a.actId,1)">
				<view style="width: 200rpx;height: 160rpx;"><img style="width: 200rpx;height: 160rpx;"
						:src="a.actAvatar" alt="">
				</view>
				<view style="width: 65%;">
					<view>{{a.actName}}</view>
					<view style="font-size: smaller;color: #808080;margin-top: 10rpx;margin-bottom: 10rpx;">
						{{a.actOrganizationName}}
					</view>
					<view
						style="display: flex;justify-content: space-between;font-size: smaller;color: #808080;margin-top: 40rpx;">
						<view>{{a.actViews}}阅读</view>
						<view>{{a.createAt}}</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 分段器
				items: ['新闻', '活动'],
				current: 0,
				pageRequest: {
					pageNo: 0,
					pageSize: 10,
					title: null,
					name: null
				},
				newsInfo: [],
				activityInfo: []
			}
		},
		onLoad() {
			this.getNews();
		},
		onShow() {
			// 检查消息
			this.check.checkMessage(this)
		},
		methods: {
			// 页面跳转
			navigateTo(id, type) {
				var url = null;
				if (type == 0) {
					url = './newsDetail?newId=' + id;
				} else if (type == 1) {
					url = './activityDetail?actId=' + id;
				}
				uni.navigateTo({
					url: url
				})
			},
			// 分段器
			onClickItem(index) {
				if (this.current !== index.currentIndex) {
					this.current = index.currentIndex;
				}
				this.pageRequest.pageNo = 0;
				if (this.current == 0) {
					this.pageRequest.title = null;
					this.newsInfo = [];
					this.getNews();
				} else if (this.current = 1) {
					this.pageRequest.name = null;
					this.activityInfo = [];
					// 获取活动列表
					this.getActivities();
				}
			},
			// 搜索
			search(res) {
				this.pageRequest.pageNo = 0;
				if (this.current == 0) {
					this.pageRequest.title = res.value
					this.newsInfo = [];
					this.getNews();
				} else if (this.current = 1) {
					this.pageRequest.name = res.value
					this.activityInfo = [];
					// 搜索活动
					this.getActivities();
				}
			},
			// 重置
			reset() {
				this.pageRequest.pageNo = 0;
				this.pageRequest.title = null;
				this.pageRequest.name = null;
				if (this.current == 0) {
					// 获取新闻列表
					this.newsInfo = [];
					this.getNews();
				} else if (this.current == 1) {
					this.activityInfo = [];
					this.getActivities()
					// 获取活动列表
				}
			},
			// 获取数据
			getNews() {
				this.pageRequest.pageNo++;
				this.$http.request({
					url: '/organizations/web/news/page',
					method: 'POST',
					data: this.pageRequest
				}).then(res => {
					res = res.data;
					if (res.result) {
						//this.newsInfo = res.data
						this.newsInfo = [
							...this.newsInfo,
							...res.data
						]
					} else {
						uni.showToast({
							title: res.data,
							icon: 'none'
						});
					}
				})
			},
			// 获取活动
			getActivities() {
				this.pageRequest.pageNo++;
				var url = null;
				if (this.pageRequest.name == null) {
					url = `/organizations/web/activity/page/${this.pageRequest.pageNo}/${this.pageRequest.pageSize}`
				} else {
					url =
						`/organizations/web/activity/page/${this.pageRequest.pageNo}/${this.pageRequest.pageSize}/${this.pageRequest.name}`
				}
				this.$http.request({
					url: url
				}).then(res => {
					res = res.data;
					if (res.result) {
						this.activityInfo = [
							...this.activityInfo,
							...res.data
						]
					} else {
						uni.showToast({
							title: res.data,
							icon: 'none'
						});
					}
				})
			},
			// 页面下拉到底部
			onReachBottom() {
				// 当前页+1
				if (this.current == 0) {
					this.getNews()
				} else if (this.current == 1) {
					this.getActivities()
				}
			}
		}
	}
</script>
<style>
</style>
